package com.example.bailin.giftsay.sort.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.bailin.giftsay.R;
import com.example.bailin.giftsay.entity.SortColumnEntity;
import com.example.bailin.giftsay.sort.LmClickListener;
import com.example.bailin.giftsay.tool.MyApp;
import com.example.bailin.giftsay.tool.NetTool;

/**
 * Created by bailin on 16/8/19.
 *
 * 栏目的二级页面适配器
 */
public class SortColumnAdapter extends BaseAdapter {
    private SortColumnEntity entity;
    private LmClickListener lmClickListener;

    public void setLmClickListener(LmClickListener lmClickListener) {
        this.lmClickListener = lmClickListener;
    }

    public void setEntity(SortColumnEntity entity) {
        this.entity = entity;
    }

    @Override
    public int getCount() {
        return entity.getData().getColumns() == null ? 0 : entity.getData().getColumns().size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(MyApp.getContext());
        SortColumnViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_sortcolumn, parent, false);
            viewHolder = new SortColumnViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (SortColumnViewHolder) convertView.getTag();
        }
        NetTool tool = new NetTool();
        try {
            tool.getImg(entity.getData().getColumns().get(position).getCover_image_url(), viewHolder.ivSortColumnCoverImageUrl);
            viewHolder.tvSortColumnTitle.setText(entity.getData().getColumns().get(position).getTitle());
            viewHolder.tvSortColumnSubtitle.setText(entity.getData().getColumns().get(position).getSubtitle());
            viewHolder.tvSortColumnAuthor.setText(entity.getData().getColumns().get(position).getAuthor());
        } catch (NullPointerException e) {
        }
        viewHolder.rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = entity.getData().getColumns().get(position).getTitle();
                int id = entity.getData().getColumns().get(position).getId();
                lmClickListener.onClick(id, title);
            }
        });
        return convertView;
    }

    private class SortColumnViewHolder {
        public ImageView ivSortColumnCoverImageUrl; //大图片
        public TextView tvSortColumnTitle; // 标题
        public TextView tvSortColumnSubtitle; // 更新日期
        public TextView tvSortColumnAuthor; // 作者姓名
        public RelativeLayout rl;
        public SortColumnViewHolder(View v) {
            ivSortColumnCoverImageUrl = (ImageView) v.findViewById(R.id.iv_sortcolumn_cover_image_url);
            tvSortColumnTitle = (TextView) v.findViewById(R.id.tv_sortcolumn_title);
            tvSortColumnSubtitle = (TextView) v.findViewById(R.id.tv_sortcolumn_subtitle);
            tvSortColumnAuthor = (TextView) v.findViewById(R.id.tv_sortcolumn_author);
            rl = (RelativeLayout) v.findViewById(R.id.rl_lm_click);
        }
    }
}
